package com.swm.util1;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;

/**
 * @author 师文明
 * @data 2025/5/13 下午2:53
 **/
public class API {
    public static String getDCWebData (String code,int type){
        String data = null;
        try {
            //百度股票接口
            //String url = "https://finance.pae.baidu.com/vapi/v1/getquotation?group=quotation_kline_ab&code="+code+"&ktype=day";
            // 连接到新浪财经接口
            String url = "https://push2his.eastmoney.com/api/qt/stock/kline/get?fields1=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13,f116,f117&fields2=f51,f52,f53,f54,f55,f56,f57,f58,f59,f60,f61&beg=0&end=20500101&rtntype=6&secid=" + type + "." + code + "&klt=101&fqt=1";
            URL obj = new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
            con.setRequestMethod("GET");
            con.setRequestProperty("Cookie", "qgqp_b_id=0c801f7a5b2dbbe1a7578a4f50bf5c81; websitepoptg_api_time=1747117149175; st_si=68879956819867; st_asi=delete; HAList=ty-0-872351-%u534E%u5149%u6E90%u6D77; fullscreengg=1; fullscreengg2=1; st_pvi=68305363311862; st_sp=2025-05-13%2014%3A19%3A09; st_inirUrl=https%3A%2F%2Fwww.baidu.com%2Flink; st_sn=2; st_psi=20250513141910957-113200319503-4928320619");
            // 设置连接超时时间为 10 秒
            con.setConnectTimeout(10000);
            // 设置读取超时时间为 10 秒
            con.setReadTimeout(10000);
            // 从数据源获取实时股票数据
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8));
            String inputLine;
            StringBuffer response = new StringBuffer();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
            con.disconnect();
            data = response.toString();
        } catch (Exception e) {
            //throw new RuntimeException(e);
            //e.printStackTrace();
        }
        return data;
    }

    public static String getBDWebData (String code,int type){
        String data = null;
        try {
            //百度股票接口
            String url = "https://finance.pae.baidu.com/vapi/v1/getquotation?group=quotation_kline_ab&code="+code+"&ktype=day";
            URL obj = new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
            con.setRequestMethod("GET");
            con.setRequestProperty("Cookie", "BIDUPSID=8D857A34DDE5A49F060246964243DCB1; PSTM=1747011027; BAIDUID=8D857A34DDE5A49F060246964243DCB1:FG=1; MCITY=-268%3A; BDSFRCVID=MxuOJexroGWN3M5sKssVEHtYogKK0gOTDYrE_kM3LlJP3rLVvfe5EG0PtOi2xEPM4ch-ogKKBeOTHn0_FmL2QHkU8N2fp7fDul64tf8g0M5; H_BDCLCKID_SF=tbkD_C-MfIvDqTrP-trf5DCShUFsBjoJB2Q-XPoO3M3PhRbCKURc5tKkyhOk2-biWbRM2MbgylRp8P3y0bb2DUA1y4vp-fjX-mTxoUJ25DnJjlCzqfCWMR-ebPRi3tQ9QgbXoU7Ial4KVqnc3j7FD4KQjUjR-4AqJTnhVn0MBCK0MDt4jjuaejQM-l-X5to05TIX3b7Ef-8aOq7_bJ7KhU-iXGbutRJi-573Kln2yR7JOpkxbUnxQhFXQtnfXpOe-n7rKhc1QtQteD5HQT3m5M4tDl74QUuJKK-LWb3cWhoq8UbSh63rQ4uTDRQ7ahLOBJrhQI3D3P5SMCbmb67JDMr0eG_jJjL8tRKtVbobHJoHjJbGq4bohjPR0pO9BtQO-DOx-hubBC55OPORjfo15hFebPRiJPb9QgnkQq5vbMnmqPtRXMJkXhKBbx_t0x-jLTn9BCoy3xJYMbOdqtnJyUPYbtnnBPCjX2cL2IolWnvlbhu4bR0bbx00haDJ-PKO5bRu_CFatI_hbDDwjjRb5nbHbMnt5toyHD7yWCv7LtbcOR59K4nn3U6yXHoeBbJeJmT7LUTO5hvvhb3O3MOZKxLmMPFeWP7ZLn7H2UQK5JKhsq0x0bOcMjDbQJ3utPJ3HCOMahvP5l7xObuxBUjVX5DuyMRPqUCH5j4f0qbaaILMfbL9bT3YjjT0DaKtq6_8JR3fL-085n7hHRjvq4bohjPI2ho9BtQO-DOx-hubBC55OPOR3xo15hFebPRiJPb9QgnkQq572hC58bR_05bKXhKBbx_t0x-jLTn9BCoy3xJYMbOdqtnJyUPYbtnnBPCjX2cL2IolWnvlbhu4bR0bbx00haDJ-PKO5bRu_CcJ-J8XMK-Gjj5P; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; H_PS_PSSID=60274_61027_61678_62484_62327_62889_62969_63042_63047_63143_63111_63194_63210_63244_63248_63254; delPer=0; PSINO=2; BAIDUID_BFESS=8D857A34DDE5A49F060246964243DCB1:FG=1; BDSFRCVID_BFESS=MxuOJexroGWN3M5sKssVEHtYogKK0gOTDYrE_kM3LlJP3rLVvfe5EG0PtOi2xEPM4ch-ogKKBeOTHn0_FmL2QHkU8N2fp7fDul64tf8g0M5; H_BDCLCKID_SF_BFESS=tbkD_C-MfIvDqTrP-trf5DCShUFsBjoJB2Q-XPoO3M3PhRbCKURc5tKkyhOk2-biWbRM2MbgylRp8P3y0bb2DUA1y4vp-fjX-mTxoUJ25DnJjlCzqfCWMR-ebPRi3tQ9QgbXoU7Ial4KVqnc3j7FD4KQjUjR-4AqJTnhVn0MBCK0MDt4jjuaejQM-l-X5to05TIX3b7Ef-8aOq7_bJ7KhU-iXGbutRJi-573Kln2yR7JOpkxbUnxQhFXQtnfXpOe-n7rKhc1QtQteD5HQT3m5M4tDl74QUuJKK-LWb3cWhoq8UbSh63rQ4uTDRQ7ahLOBJrhQI3D3P5SMCbmb67JDMr0eG_jJjL8tRKtVbobHJoHjJbGq4bohjPR0pO9BtQO-DOx-hubBC55OPORjfo15hFebPRiJPb9QgnkQq5vbMnmqPtRXMJkXhKBbx_t0x-jLTn9BCoy3xJYMbOdqtnJyUPYbtnnBPCjX2cL2IolWnvlbhu4bR0bbx00haDJ-PKO5bRu_CFatI_hbDDwjjRb5nbHbMnt5toyHD7yWCv7LtbcOR59K4nn3U6yXHoeBbJeJmT7LUTO5hvvhb3O3MOZKxLmMPFeWP7ZLn7H2UQK5JKhsq0x0bOcMjDbQJ3utPJ3HCOMahvP5l7xObuxBUjVX5DuyMRPqUCH5j4f0qbaaILMfbL9bT3YjjT0DaKtq6_8JR3fL-085n7hHRjvq4bohjPI2ho9BtQO-DOx-hubBC55OPOR3xo15hFebPRiJPb9QgnkQq572hC58bR_05bKXhKBbx_t0x-jLTn9BCoy3xJYMbOdqtnJyUPYbtnnBPCjX2cL2IolWnvlbhu4bR0bbx00haDJ-PKO5bRu_CcJ-J8XMK-Gjj5P; H_WISE_SIDS=60274_61027_61678_62484_62327_62889_62969_63042_63047_63143_63111_63194_63210_63244_63248_63254; BDRCVFR[rYCNwgpEuDR]=mk3SLVN4HKm; BA_HECTOR=0lah8h2l8la485a52g04a1akajdvim1k25qma22; ZFY=tn531LaADzyon7XouCr3F9B9g2c3fTCpZHwCTvoNahE:C; ab_sr=1.0.1_NTUwNDIyODM2NzM4YjUxYTRhMmVkNTdmMzkxZDFmNTRhMjAzYzU4MzQ0NTY1MzM2NDMwOGFjMjBhMTBhOWUzNDVjYzYxMGE4MDdkMjU2NzMyOTg3YTBjMDdmNjE1N2U1OGUzNTRhMGI3YmUxNTIwMjY3NTFmODU0NzkzOTlkZjAwYzFjNzFhZTZlMDFhNDcxYTljNTg1ODY2NjgxZjE4NzdkNjEwZGQ5OThkZGMzNWRlMmJiZGIyOWY3MzdjODJlN2M0OTY3ZDc3N2MzNWM3ZTM5ZjIxNTQ2M2VkZWRhNWU=");

            // 从数据源获取实时股票数据
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), StandardCharsets.UTF_8));
            String inputLine;
            StringBuffer response = new StringBuffer();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
            con.disconnect();
            data = response.toString();
        } catch (Exception e) {
            //throw new RuntimeException(e);
            //e.printStackTrace();
        }
        return data;
    }
}
